﻿#include "operation.h"
#include"sort.h"
bool initSys(Sys sys)
{
	if (!sys)return false;
	sys->size = 3;//航线数量设为3
	strcpy(sys->airline[0].beg, "广州");
	strcpy(sys->airline[0].dest, "上海");
	sys->airline[0].workDay[0] = 3;
	sys->airline[0].workDay[1] = 4;
	sys->airline[0].workDay[2] = 5;

	strcpy(sys->airline[1].beg, "北京");
	strcpy(sys->airline[1].dest, "上海");
	sys->airline[1].workDay[0] = 0;
	sys->airline[1].workDay[1] = 1;
	sys->airline[1].workDay[2] = 2;

	strcpy(sys->airline[2].beg, "广州");
	strcpy(sys->airline[2].dest, "重庆");
	sys->airline[2].workDay[0] = 2;
	sys->airline[2].workDay[1] = 4;
	sys->airline[2].workDay[2] = 6;
	printf("现有航线：\n");
	for (int i = 0; i < AllLine; i++)
	{

		printf("%s---%s\n",sys->airline[i].beg, sys->airline[i].dest);
	}
	//测试用例
	//第一条航线
	pFlight pNew, temp = NULL;
	for (int k = 0; k < 3; k++)
	{
		pNew = (pFlight)malloc(sizeof(Flight));
		memset(pNew, 0, sizeof(Flight));
		pNew->leftNum = pNew->totalNum = 300;
		for (int i = 0; i < 3; i++)
			pNew->level[i].leftNum = pNew->level[i].totalNum = 100;
		pNew->number = 7000+k;
		pNew->planeID = 10+k;
		pNew->startDay = 3+k;
		pNew->next = temp;
		temp = pNew;
	}
	sys->airline[0].flights = temp;
	//第二条航线
	pNew = temp = NULL;
	for (int k = 0; k < 3; k++)
	{
		pNew = (pFlight)malloc(sizeof(Flight));
		memset(pNew, 0, sizeof(Flight));
		pNew->leftNum = pNew->totalNum = 300;
		for (int i = 0; i < 3; i++)
			pNew->level[i].leftNum = pNew->level[i].totalNum = 100;
		pNew->number = 8500 + k;
		pNew->planeID = 10 + k;
		pNew->startDay = 0 + k;
		pNew->next = temp;
		temp = pNew;
	}
	sys->airline[1].flights = temp;
	//第三条
	sys->airline[2].flights = NULL;
	sortLine(sys);
	for (int i = 0; i < AllLine; i++)
	{
		sys->airline[i].flights = sortFlight(sys->airline[i].flights);
	}
	return true;
}

void checkLine(Sys sys)
{
	if (!sys)return;
	printf("请输入要查找的航班终点：");
	char dest[20] = {};
	scanf("%s", dest);
	while (getchar() != '\n');
	bool isfind = false;
	for (int i = 0; i < AllLine; i++)
	{
		if (strcmp(sys->airline[i].dest, dest) == 0)
		{
			isfind = true;
			pFlight pos = sys->airline[i].flights;
			while (pos)
			{
				printf("查找到航班%d起点：%s,终点：%s,起飞日期：星期%d,飞机编号：%d,剩余一等舱票数：%d，剩余二等舱票数：%d，剩余三等舱票数：%d\n",
					pos->number,sys->airline[i].beg, sys->airline[i].dest, pos->startDay, pos->planeID,pos->level[0].leftNum, pos->level[1].leftNum, pos->level[2].leftNum);
					pos = pos->next;
			}
		}
	}
	if (!isfind)
	{
		printf("暂时没有符合的航班信息\n");
	}
	
}
void book(Sys sys)
{


	if (!sys)return;
	checkLine(sys);
	printf("请输入要预订的航班号：");
	int fId;
	scanf("%d", &fId);
	while (getchar() != '\n');
	printf("请输入要预订的票数：");
	int tnum;
	scanf("%d", &tnum);
	while (getchar() != '\n');
	bool isfind = false;
	pFlight pos=NULL;
	int i;
	for (i = 0; i < sys->size; i++)
	{
		pos = sys->airline[i].flights;
		while (pos!=NULL&&pos->number != fId)
		{
			pos = pos->next;
		}

		if (!pos)continue;
		if (pos->number == fId)
		{
			isfind = true;
			break;
		}
	}
	if (!isfind)
	{
		printf("找不到该航班，请重新查询或退出本系统\n");
		return;
	}
	printf("请输入您想要预订的舱位等级：（请输入 1，2或3）");
	int lv = 0;
	scanf("%d", &lv);
	while (getchar() != '\n');
	if (pos->level[lv-1].leftNum < tnum)
	{
		printf("该航班%d等票剩余票数少于您的需要的票数，是否候补：（Y/N）",lv);
		char ch;
		scanf("%c", &ch);
		while (getchar() != '\n');
		if (ch == 'Y' || ch == 'y')
		{
			char name[20] = {};
			printf("请输入您的姓名：");
			scanf("%s", name);
			while (getchar() != '\n');
			char idCard[20] = {};
			printf("请输入您的身份证号码:");
			scanf("%s", idCard);
			while (getchar() != '\n');
			waitforTicket(pos, tnum, lv, name, idCard);
		}
		else
		return;
	}
	else
	{
		char name[20] = {};
		printf("请输入您的姓名：");
		scanf("%s", name);
		while (getchar() != '\n');
		char idCard[20] = {};
		printf("请输入您的身份证号码:");
		scanf("%s", idCard);
		while (getchar() != '\n');
		
		printf("剩余票数足够，已为您预订了从%s到%s的%d航班的%d张%d等票，请准备在星期%d到机场乘坐飞机\n",
			sys->airline[i].beg, sys->airline[i].dest, pos->number, tnum, lv , pos->startDay);
		pos->leftNum -= tnum;//剩余票数要减
		pos->level[lv - 1].leftNum -= tnum;

		//插入已订购名单中（采用头插法）
		BookedList temp = pos->booked;
		pos->booked = (BookedList)malloc(sizeof(BookNode));
		pos->booked->next = temp;
		temp = pos->booked;
		strcpy(temp->idCard, idCard);
		temp->level = lv;
		strcpy(temp->name, name);
		temp->TicketNum = tnum;
		pos->booked=sortBookList(pos->booked);//排序
	}
}

void waitforTicket(pFlight pos,int tnum,int level,char name[20],char idCard[20])
{
	//将该乘客插入候补名单中
	waitingList pNew = (waitingList)malloc(sizeof(waitingNode));
	pNew->wantNum = tnum;
	strcpy(pNew->name, name);
	strcpy(pNew->idCard, idCard);
	pNew->level = level;
	if (pos->rear == NULL)
	{
		pNew->next = NULL;
		pos->rear = pos->front = pNew;
	}
	else
	{
		pos->rear->next = pNew;
		pos->rear = pos->rear->next;
		pos->rear->next = NULL;
	}

}

void refund(Sys sys)
{
	if (!sys)return;
	printf("请输入您的身份证号码：");
	char idCard[20] = {};
	scanf("%s", idCard);
	while (getchar() != '\n');
	bool isfind = false;
	pFlight pos;
	BookedList temp,preTemp=NULL;
	for (int i = 0; i < sys->size; i++)
	{

		pos = sys->airline[i].flights;
		if (!pos)continue;
		temp = pos->booked;
		while (temp)
		{
			if (0==strcmp(temp->idCard, idCard))
			{
				isfind = true;
				printf("你预订了%d航班的%d等舱%d票\n", pos->number, temp->level, temp->TicketNum);
				break;
			}
			preTemp = temp;
			temp = temp->next;
		}
		if (NULL != temp)break;
		pos = pos->next;
	}
	if (!temp)//没有找到
	{
		printf("没有找到您的订票记录\n");
		return;
	}
	printf("是否退票？（Y/N）");
	char ch;
	scanf("%c", &ch);
	while (getchar() != '\n');
	if (ch == 'N' || ch == 'n')return;
	else if (ch == 'Y' || ch == 'y')
	{
		//将记录从已订记录中删除
		if (preTemp == NULL)
		{
			pos->booked = temp->next;
		}
		else
		{
			preTemp->next = temp->next;
		}
		//余票数加上退票数
		pos->leftNum += temp->TicketNum;
		pos->level[temp->level - 1].leftNum += temp->TicketNum;
		free(temp);
		printf("已为您退票。\n");
	}
	//重新检查有没有人符合候补要求
	waitingList pW = pos->front;
	waitingList preW = NULL;
	while (pW)
	{
		if (pW->wantNum <= pos->level[pW->level - 1].leftNum)
		{
			//该乘客候补成功，将发送消息提醒乘车
			printf("乘客%s身份证号%s候补成功\n",pW->name,pW->idCard);
			//在买票客户中添加该乘客
			BookedList pNew = (BookedList)malloc(sizeof(BookNode));
			strcpy(pNew->idCard, pW->idCard);
			strcpy(pNew->name, pW->name);
			pNew->level = pW->level;
			pNew->TicketNum = pW->wantNum;
			pNew->next = pos->booked;
			pos->booked = pNew;
			//票数减少
			pos->leftNum -= pW->wantNum;
			pos->leftNum -= pW->wantNum;
			//候补队列删除该乘客
			if (pos->front == pos->rear)//如果只有一个
			{
				pos->front = pos->rear = NULL;
			}
			else if (NULL == preW)//该乘客位于队头
			{
				pos->front = pos->front->next;
			}
			else if(pW==pos->rear)//在队列尾
			{ 
				preW->next = NULL;
				pos->rear = preW;
			}
			else//在中间
			{
				preW->next = pW->next;
			}
			if (!preW)
			{
				free(pW);
				return;
			}
			waitingList t = preW->next;
			free(pW);
			pW = t;
			continue;
		}
		preW = pW;
		pW = pW->next;
	}


}

void show(BookedList b)
{
	if (!b)return;
	BookedList temp = b;
	while (temp)
	{
		printf("身份证号：%s,姓名：%s,订购机票数量：%d,舱位：%d\n", temp->idCard,temp->name,temp->TicketNum,temp->level);
		temp = temp->next;
	}
	b = b->next;
}

void destroy(Sys sys)
{
	pFlight pos;
	for (int i = 0; i < AllLine; i++)
	{
		
		pos = sys->airline->flights;
		if (!pos)continue;
		while (pos)
		{
			BookedList b = pos->booked;
			while (b)
			{
				b = b->next;
				free(pos->booked);
				pos->booked = b;
			}
		}
		waitingList w = pos->front;
		while (w)
		{
			w = w->next;
			free(pos->front);//逐个出队，释放
			pos->front = w;
		}
		sys->airline->flights = pos->next;
		free(pos);
	}
}



